<?php

/* * Main class */

final class Index {

    const DEFAULT_PAGE = 'home';
    const PAGE_DIR = '../view/';
    const LAYOUT_DIR = '../layout/';

    /**
     * Run point of application
     */
    public function run() {
        $this->runPage($this->getPage());
    }

    /**
     * System Config: pass method that requires auto-loading for performance purposes
     */
    public function init() {
        set_exception_handler(array($this, 'handleException'));
        spl_autoload_register(array($this, 'loadClass'));
    }

    /**
     * Exception Handler
     */
    public function handleException(Exception $ex) {
        $extra = array('message' => $ex->getMessage());
        if ($ex instanceof NotFoundException) {
            header('HTTP/1.0 404 Not Found');
            $this->runPage('404', $extra);
        } else {
            header('HTTP/1.1 500 Internal Server Error');
            $this->runPage('500', $extra);
        }
    }

    /**
     * Class loader
     */
    public function loadClass($name) {
        $classes = array(
            'Config' => '../utils/Config.php',
            'DatabaseConnection' => '../database/DatabaseConnection.php',
            'Error' => '../utils/Error.php',
            'NotFoundException' => '../utils/exception/NotFoundException.php',
            'Utils' => '../utils/Utils.php',
            'BaseDAO' => '../dao/BaeDAO.php',
            'SuggestionDAO' => '../dao/SuggestionDAO.php',
            'ViewSuggestionListController' => '../controller/ViewSuggestionListController.php',
            'Suggestion' => '../model/Suggestion.php',
            'Content'=>'../model/Content.php',
            'ContentType'=>'../utils/ContentType.php',
            'ViewCommand' => '../model/ViewCommand.php',
        );
        if (!array_key_exists($name, $classes)) {
            die('Class "' . $name . '" not found!');
        }
        require_once $classes[$name];
    }

    private function getPage() {
        $page = self::DEFAULT_PAGE;
        if (array_key_exists('page', $_GET)) {
            $page = $_GET['page'];
        }
        return $this->checkPage($page);
    }

    private function checkPage($page) {
        if (!preg_match('/^[a-z0-9-]+$/i', $page)) {
            // TODO log attempt, redirect attacker, ...
            throw new NotFoundException('Unsafe page "' . $page . '" requested');
        }
        if (!$this->hasScript($page) && !$this->hasTemplate($page)) {
            // TODO log attempt, redirect attacker, ...
            throw new NotFoundException('Page "' . $page . '" not found');
        }
        return $page;
    }

    private function runPage($page, array $extra = array()) {
        $run = false;
        if ($this->hasScript($page)) {
            $run = true;
            require $this->getScript($page);
        }
        if ($this->hasTemplate($page)) {
            $run = true;
            // data for main template
            $template = $this->getTemplate($page);

            // main template (layout)
            require self::LAYOUT_DIR . 'index.phtml';
        }
        if (!$run) {
            die('Page "' . $page . '" has neither script nor template!');
        }
    }

    private function getScript($page) {
        return self::PAGE_DIR . $page . '.php';
    }

    private function getTemplate($page) {
        return self::PAGE_DIR . $page . '.phtml';
    }

    private function hasScript($page) {
        return file_exists($this->getScript($page));
    }

    private function hasTemplate($page) {
        return file_exists($this->getTemplate($page));
    }

}

$index = new Index();
$index->init();
// run application!
$index->run();
?>
